package com.yjd.comm.system.util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.cache.up.ICacheUpdate;
import com.yjd.comm.system.enums.DictTypeEnum;
import com.yjd.comm.system.model.LinkageModel;
import com.yjd.comm.system.service.ILinkageService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServiceHolder;

/**
 * @ClassName: DictUtil
 * @Description: 数据字典工具类
 * @author ready likun_557@163.com
 * @date 2014-8-11 下午5:02:24
 */
public class DictUtil implements ICacheUpdate {

	private static Logger logger = Logger.getLogger(DictUtil.class);
	private static final String SERVICE = "linkageService";

	/**
	 * 禁止直接访问该map的值,通过getAllDicts()来访问
	 */
	private static Map<String, List<LinkageModel>> map = null;

	private static ILinkageService getDictService() {
		return ServiceHolder.getService(ILinkageService.class, SERVICE);
	}

	/**
	 * 根据字典的类型获取字典的列表
	 * 
	 * @param dictTypeEnum
	 * @return
	 * @throws Exception
	 */
	public static List<LinkageModel> getDicts(DictTypeEnum dictTypeEnum)
			throws Exception {
		Map<String, List<LinkageModel>> dictMap = getAllDicts();
		if (dictMap != null) {
			List<LinkageModel> tempList = dictMap.get(dictTypeEnum
					.getDictType());
			if (tempList == null) {
				return new ArrayList<LinkageModel>();
			}
			List<LinkageModel> list = new ArrayList<LinkageModel>();
			for (LinkageModel dictModel : tempList) {
				list.add(dictModel);
			}
			return list;
		}
		return null;
	}

	/**
	 * 根据字典的类型获取字典列表，key为字典的value
	 * 
	 * @param dictTypeEnum
	 * @return
	 * @throws Exception
	 */
	public static Map<String, LinkageModel> getDictsMap(
			DictTypeEnum dictTypeEnum) throws Exception {
		List<LinkageModel> list = getDicts(dictTypeEnum);
		if (list != null) {
			Map<String, LinkageModel> map = new LinkedHashMap<String, LinkageModel>();
			for (LinkageModel linkageModel : list) {
				map.put(linkageModel.getValue(), linkageModel);
			}
			return map;
		}
		return null;
	}

	/**
	 * 获取字典所有数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Map<String, List<LinkageModel>> getAllDicts()
			throws Exception {
		if (map == null) {
			synchronized (DictUtil.class) {
				if (map == null) {
					init();
				}
			}
		}
		return map;
	}

	/**
	 * 此方法可以避免map在并发情况下，数据读取为空的情况
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private static void init() throws Exception {
		synchronized (DictUtil.class) {
			Map<String, List<LinkageModel>> tempMap = new LinkedHashMap<String, List<LinkageModel>>();
			List<LinkageModel> list = getDictService().getModelList(
					FrameUtil.newHashMap("sort", "pid asc"),DbWREnums.READ);
			for (LinkageModel linkageModel : list) {
				Object key = linkageModel.getCode();
				List<LinkageModel> temp = tempMap.get(key);
				if (temp == null) {
					temp = new ArrayList<LinkageModel>();
				}
				temp.add(linkageModel);
				tempMap.put(key != null ? key.toString() : null, temp);
			}
			map = tempMap;
		}
	}

	/**
	 * 根据dict的类型和code获取dict的描述信息
	 * 
	 * @param dictTypeEnum
	 * @param dictCode
	 * @return
	 * @throws Exception
	 */
	public static String getDictDescByCode(DictTypeEnum dictTypeEnum,
			Object dictCode) throws Exception {
		if (dictCode == null) {
			return null;
		}
		LinkageModel dictModel = getDictByCode(dictTypeEnum, dictCode);
		return dictModel == null ? null : dictModel.getName();
	}

	/**
	 * 根据dict的类型和code获取dict的信息信息
	 * 
	 * @param dictTypeEnum
	 * @param dictCode
	 * @return
	 * @throws Exception
	 */
	public static LinkageModel getDictByCode(DictTypeEnum dictTypeEnum,
			Object dictCode) throws Exception {
		Map<String, List<LinkageModel>> dictMap = getAllDicts();
		List<LinkageModel> dictModels = dictMap.get(dictTypeEnum.getDictType());
		if (dictModels != null) {
			for (LinkageModel model : dictModels) {
				if ((model.getValue() != null && dictCode == null)
						|| model.getValue().toString()
								.equals(dictCode.toString())) {
					return model;
				}
			}
		}
		return null;
	}

	@Override
	public void updateCache() {
		try {
			init();
			logger.info("更新数据字典缓存成功!");
		} catch (Exception e) {
			logger.error("更新数据字典缓存出错:" + e.getMessage(), e);
		}
	}

}
